package cn.lu.train.base;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseDialogFragment extends DialogFragment {

    private int mBackgroundColor = Color.TRANSPARENT;
    private @DrawableRes int mBackgroundRes;
    private boolean isBackgroundRes;

    private Unbinder mUnbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onCreated();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutRes(), container);
        mUnbinder = ButterKnife.bind(this, view);
//        setStyle(STYLE_NORMAL, R.style.SliderDialog);
        onCreated(view);
        initDialog();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    @Override
    public void onStart() {
        super.onStart();

        // 设置背景透明
        Window w = getDialog().getWindow();
        if (null != w) {
            if (isBackgroundRes) {
                w.setBackgroundDrawableResource(mBackgroundRes);
            } else {
                w.setBackgroundDrawable(new ColorDrawable(mBackgroundColor));
            }
            setAttributes(w.getAttributes());
        }

    }

    private void initDialog() {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    public abstract int getLayoutRes();

    public void onCreated(View layout) {}

    public void setBackground(@ColorInt int background) {
        this.mBackgroundColor = background;
        isBackgroundRes = false;
    }

    public void setBackgroundResource(@DrawableRes int backgroundRes) {
        this.mBackgroundRes = backgroundRes;
        isBackgroundRes = true;
    }

    protected void setAttributes(WindowManager.LayoutParams params) {
        Window window = getDialog().getWindow();
        if (window != null) {
            window.setAttributes(params);
        }
    }

    public void onCreated() {

    }

    public void initData() {

    }

    public void showLoading() {

    }

    public void hiddenLoading() {

    }

    public void show(FragmentManager fm) {
        show(fm, "@" + hashCode());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}
